<template>
  <div class="music-list">
    <div class="header">
      <h2 class="list-title">播放列表</h2>
      <button 
        class="btn-vip-toggle" 
        :class="{ active: isVip }"
        @click="toggleVipStatus"
      >
        {{ isVip ? '取消会员' : '开通会员' }}
      </button>
    </div>
    <div class="list-container">
      <div 
        v-for="(song, index) in playlist" 
        :key="song.id"
        class="song-item" 
        :class="{ active: index === currentIndex }"
        @click="playSong(index)"
      >
        <div class="song-rank">{{ index + 1 }}</div>
        <div class="song-info">
          <div class="title-container">
            <h4 class="song-title">{{ song.title }}</h4>
            <span v-if="song.isPremium" class="premium-tag">VIP</span>
          </div>
          <p class="song-artist">{{ song.artist }}</p>
        </div>
        <div class="song-actions">
          <button class="btn-play-item" @click.stop="togglePlay(index)">
            {{ index === currentIndex && isPlaying ? '⏸️' : '▶️' }}
          </button>
          <button class="btn-remove" @click.stop="removeSong(song.id)">🗑️</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useMusicStore } from '../stores/music.js'

const store = useMusicStore()

// 计算属性
const playlist = computed(() => store.playlist)
const currentIndex = computed(() => store.currentIndex)
const isPlaying = computed(() => store.isPlaying)
const isVip = computed(() => store.isVip)

// 切换会员状态（仅用于演示）
const toggleVipStatus = () => {
  store.toggleVip()
}

// 播放指定歌曲
const playSong = (index) => {
  if (index !== currentIndex.value) {
    // 如果点击的是不同的歌曲，则切换到该歌曲并开始播放
    store.currentIndex = index
    store.isPlaying = true
    // 这里需要通知播放器组件更新播放源
    window.dispatchEvent(new Event('songChanged'))
  } else {
    // 如果点击的是当前歌曲，则切换播放/暂停状态
    togglePlay(index)
  }
}

// 切换播放/暂停状态
const togglePlay = (index) => {
  if (index === currentIndex.value) {
    store.togglePlay()
  }
}

// 移除歌曲
const removeSong = (songId) => {
  if (confirm('确定要从播放列表中移除这首歌吗？')) {
    store.removeSong(songId)
  }
}
</script>

<style scoped>
.music-list {
  background: #fff5f5;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  max-width: 600px;
  margin: 0 auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.list-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: #333;
}

.btn-vip-toggle {
  padding: 8px 16px;
  background: #ff9800;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.btn-vip-toggle:hover {
  background: #f57c00;
}

.btn-vip-toggle.active {
  background: #e91e63;
}

.btn-vip-toggle.active:hover {
  background: #c2185b;
}

.list-container {
  max-height: 400px;
  overflow-y: auto;
}

.song-item {
  display: flex;
  align-items: center;
  padding: 15px;
  margin-bottom: 10px;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.song-item:hover {
  transform: translateX(5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.song-item.active {
  background: linear-gradient(90deg, #e53e3e, #c53030);
  color: white;
}

.song-rank {
  width: 30px;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  margin-right: 15px;
  color: #999;
}

.song-item.active .song-rank {
  color: #ffd700;
}

.song-info {
  flex: 1;
}

.title-container {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
}

.song-title {
  font-size: 16px;
  font-weight: 500;
  margin: 0;
  color: #333;
  flex: 1;
}

.premium-tag {
  background: #ff9800;
  color: white;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: bold;
}

.song-artist {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.song-item.active .song-title,
.song-item.active .song-artist {
  color: white;
}

.song-item.active .premium-tag {
  background: #ffffff;
  color: #ff9800;
}

.song-actions {
  display: flex;
  gap: 10px;
}

.btn-play-item,
.btn-remove {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.3s ease;
  color: #999;
}

.btn-play-item:hover,
.btn-remove:hover {
  background: rgba(0, 0, 0, 0.1);
  color: #e53e3e;
}

.song-item.active .btn-play-item,
.song-item.active .btn-remove {
  color: white;
}

.song-item.active .btn-play-item:hover,
.song-item.active .btn-remove:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* 滚动条样式 */
.list-container::-webkit-scrollbar {
  width: 6px;
}

.list-container::-webkit-scrollbar-track {
  background: #fff0f0;
  border-radius: 3px;
}

.list-container::-webkit-scrollbar-thumb {
  background: #e53e3e;
  border-radius: 3px;
}

.list-container::-webkit-scrollbar-thumb:hover {
  background: #c53030;
}
</style>